import $ from "jquery";
import ProfileMgr from "../../data/ProfileMgr.js";
function ListRender(){

}
Object.assign(ListRender.prototype,{
    init: function(selector, callback){
        this.m_ulEle = document.querySelector(selector);
        this.m_callback = callback;
    }
    ,
    // 渲染列表数据
    render: function(data){
        let html = ``;
        data.forEach((item)=>{
            html += this.createOneItem(item);
        });
        this.m_ulEle.innerHTML = html;
    }
    ,
    // 设置要渲染的总数据
    setListData: function(data){
        this.render(data);
        this.bindEvent();
    }
    ,
    // 创建单个item
    createOneItem: function(info){
        return `<li>
            <div class="pic_tu">
                <div class="photos">
                    <a href="${info.link_url}" target="_blank">
                        <img src="" data-src="${info.img}" alt="${info.goods_name}" title="${info.goods_name}">
                    </a>
                </div>

                <div class="sellpoint">
                    <div class="photo_ren"></div>
                    ${info.desc_name}
                </div>
                <div class="sellpoint_xx">
                    <div class="photo_ren"></div>
                    <div class="shang">
                        <div class="ts">${info.desc_name}</div>
                        <div class="clear"></div>
                    </div>
                    <div class="xia">
                        <a class= "add-cart" href="javascript:void(0)" data-goodsid = "${info.goods_id}">
                            <img src="//pic.winekee.com/web/add.gif">
                        </a>
                    </div>
                </div>
            </div>
            <div class="bt_wine">
                <a href="${info.link_url}" title="${info.goods_name}" target="_blank"> 
                    ${info.goods_name} 
                </a>
            </div>
            <div class="fubiao_wine">
                <a href="${info.link_url}" title="${info.en_name}" target="_blank">
                    ${info.en_name}
                </a>
            </div>
            <div class="jiage">
                <div class="lt"><em>￥</em>${info.normal_price}</div>
                <div class="clear"></div>
            </div>
        </li>`;
    }
    ,
    bindEvent : function(){
        let that = this;
        // 将商品加入到购物车中  goodsid 如果大写了，都会转为小写
        $("#con_upn_1 ul").off().on("click", ".add-cart",function(){
            ProfileMgr.addCartListById($(this).data("goodsid")); 
            that.m_callback(); 
        });
    }
})

module.exports = ListRender;